<template>
	<view class="page_box">
		<view class="head_box " v-if="0">
			<yy-custom bgColor="bg-gradual-blue">
				<block slot="backText">返回</block>
				<block slot="content">导航栏</block>
			</yy-custom>
		</view>

		<view class="content_box">
			<button
				class="cu-btn addbtn flex align-center  justify-center "
				@click="add ? confirmAdd(1) : confirmAdd(2)"
			>
				确认添加
			</button>
			<view class="head padding-lr margin-top bg-white ">
				<view class="flex justify-between padding-tb">
					<text class="f28">收货人:</text>
					<input
						class="text-right"
						type="text"
						v-model="info.name"
						value=""
						placeholder="填写收货人姓名"
						placeholder-class="ddd"
					/>
				</view>
				<view class="flex justify-between padding-tb">
					<text class="f28">联系方式:</text>
					<input
						class="text-right"
						type="number"
						value=""
						v-model="info.phone"
						placeholder="填写联系方式"
						placeholder-class="ddd"
					/>
				</view>
				<view class="flex justify-between padding-tb" @click="show = true">
					<text class="f28">收货地址:</text>
					<input
						class="text-right"
						type="text"
						value=""
						v-model="addr"
						disabled=""
						placeholder="选择收货地址"
						placeholder-class="ddd"
					/>
				</view>
				<view class="flex justify-between padding-tb">
					<text class="f28">详细地址:</text>
					<!-- <input class="text-right" type="text" value="" placeholder="填写街道/楼牌号等" placeholder-class="ddd" /> -->
					<textarea
						value=""
						v-model="info.detail"
						placeholder="填写街道/楼牌号等"
						style="width: 50vw;color: #000000;"
						placeholder-class="ddd"
					/>
				</view>
			</view>
			<view class=" padding-lr  padding-tb-xs margin-top bg-white flex justify-between align-center ">
				<text>设置为默认地址</text>
				<u-switch v-model="checked" @change="change"></u-switch>
				<!-- <v-switch class="flex justify-end" v-model="checked" size="0.5" :activeValue="1" :inactive-value="0" @change="toggleMode" ></v-switch> -->
			</view>
		</view>
		<view class="foot_box"><u-picker v-model="show" mode="region" @confirm="confirm"></u-picker></view>
		<!-- 登录提示 -->
		<yy-login v-if="false"></yy-login>
		<!-- 加载提示 -->
		<yy-loading text="加载中.." mask="true" click="true" ref="loading"></yy-loading>
		<!-- 信息提示-->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
import vSwitch from '@/components/v-switch/v-switch';
export default {
	components: {
		vSwitch
	},
	data() {
		return {
			checked: false,
			show: false,
			addr: '',
			info: {
				province: '',
				city: '',
				county: '',
				detail: '',
				name: '',
				phone: '',
				userId: uni.getStorageSync('data').id,
				isDefault: 1,
				cityCode: '',
			},
			item: {},
			add:''
		};
	},
	onShow() {},
	onLoad(e) {
		if (!this.$u.test.isEmpty(e)) {
			this.add  = 1
			this.info = JSON.parse(e.item);
			this.addr = this.info.province + this.info.city + this.info.county;
		}
		console.log(e)
	},
	methods: {
		jump(url) {
			console.log('url: ', url);
			uni.navigateTo({
				url
			});
		},
		change(status) {
			status ? (this.info.isDefault = 1) : (this.info.isDefault = 0);
			console.log(this.info.isDefault);
		},
		async confirm(e) {
			this.addr = e.province.label + e.city.label + e.area.label;
			[this.info.province, this.info.city, this.info.county, this.info.cityCode] = [
				e.province.label,
				e.city.label,
				e.area.label,
				e.province.value,
			];
			 this.info.cityCode = e.province.value
			
			console.log('eeeeeeeeeee',e.province.value)
			
		},

		async confirmAdd(e) {
			if (
				[
					this.info.province,
					this.info.city,
					this.info.county,
					this.info.detail,
					this.info.name,
					this.info.phone,
					this.info.cityCode,
				].includes('')
			) {
				return this.$refs.uToast.show({ title: '请填写完整的地址信息', type: 'error' });
			}

			if (!this.$u.test.mobile(this.info.phone)) {
				this.info.phone = '';
				return this.$refs.uToast.show({ title: '请正确填写手机号码', type: 'error' });
			}
			// 1== 修改  2== 新增

			if (e == 2) {
				const res = await this.$u.post('harvestaddress', this.info, {});

				if (res.code == 1) {
					return this.$refs.uToast.show({ title: res.msg, type: 'error' });
				}

				this.$refs.uToast.show({ title: '添加地址成功', type: 'success' });
				setTimeout(() => {
					history.back()
				}, 500);
			} else {
				console.log(this.info)
				// const res = await this.$u.put('harvestaddress', this.info, {});

				// if (res.code == 1) {
				// 	return this.$refs.uToast.show({ title: res.msg, type: 'error' });
				// }

				// this.$refs.uToast.show({ title: '添加修改成功', type: 'success' });
				// setTimeout(() => {
				// 	history.back()
				// }, 500);
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.page_box {
	// width: 100vw;
	// height: 100vh;
	// overflow: scroll;
	background-color: #f7f7f7;
	.content_box {
		.addbtn {
			position: fixed;
			bottom: 54rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 686rpx;
			height: 90rpx;
			background: #92b9b7;
			border-radius: 45rpx;

			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #ffffff;
			z-index: 9;
		}
		.head {
			.ddd {
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
				text-align: right;
			}
			/deep/input {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
			}
		}
	}
}
</style>
